package org.dromara.learning_xq.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.dromara.learning_xq.entity.AsViewingJobs;
import org.dromara.learning_xq.service.AsViewingJobsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("asViewingJobs")
@Tag(name = "作业管理（查看作业）")
public class AsViewingJobsController {


    @Autowired
    private AsViewingJobsService asViewingJobsService;


    /**
     * 查询学生作业信息
     *
     * @return 包含作业信息的响应实体
     */
    @GetMapping("/selectAsViewingJobs")
    @Operation(summary = "查询学生作业")
    public ResponseEntity<Map<String, Object>> selectAsViewingJobs() {
        Map<String, Object> response = new HashMap<>();

        try {
            // 调用服务查询学生作业列表
            List<AsViewingJobs> viewingJobsList = asViewingJobsService.selectAsViewingJobs();

            // 查询成功，准备成功响应数据
            response.put("success", true);
            response.put("data", viewingJobsList);
            response.put("message", "查询成功");
            // 返回成功响应
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 查询失败，准备失败响应数据
            response.put("success", false);
            response.put("message", "查询失败: " + e.getMessage());
            // 返回服务器错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("/selectAsViewingJobsCondition")
    @Operation(summary = "查询学生作业条件查询")
// 查询学生作业条件接口，支持根据名称和状态筛选
    public ResponseEntity<Map<String, Object>> selectAsViewingJobsCondition(
        @RequestParam(value = "name", required = false) String name,  // 名称筛选条件，可选
        @RequestParam(value = "state", required = false) String state  // 状态筛选条件，可选
    ) {
        Map<String, Object> response = new HashMap<>();

        try {
            // 调用服务查询学生作业列表
            List<AsViewingJobs> viewingJobsList = asViewingJobsService.selectAsViewingJobsCondition(state, name);

            // 检查查询结果是否为空
            if (viewingJobsList != null && !viewingJobsList.isEmpty()) {
                // 查询成功，准备成功响应数据
                response.put("success", true);
                response.put("data", viewingJobsList);
                response.put("message", "查询成功");
            } else {
                // 查询结果为空，返回没有数据的提示
                response.put("success", false);
                response.put("message", "没有符合条件的记录");
            }
        } catch (Exception e) {
            // 异常处理
            response.put("success", false);
            response.put("message", "查询过程中发生错误: " + e.getMessage());
        }
        // 返回响应
        return ResponseEntity.ok(response);
    }

    @GetMapping("/exportAsViewingJobs")
    @Operation(summary = "导出学生作业列表")
    public void exportAsViewingJobs(HttpServletResponse response) throws IOException {
        // 设置响应头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生作业列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 查询数据
        List<AsViewingJobs> viewingJobsList = asViewingJobsService.selectAsViewingJobs();

        // 导出数据到 Excel
        EasyExcel.write(response.getOutputStream(), AsViewingJobs.class)
            .excelType(ExcelTypeEnum.XLSX)
            .sheet("学生作业")
            .doWrite(viewingJobsList);
    }


}
